package com.mlm.mlmapp.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.mlm.mlmapp.R;
import com.mlm.mlmapp.activity.layoutmanager.SyGridLayoutManager;
import com.mlm.mlmapp.adapter.view.GridMarginDecoration;
import com.mlm.mlmapp.animation.RecyclAnimation;
import com.mlm.mlmapp.cons.HTTPCons;
import com.mlm.mlmapp.interfaces.Prefs_;
import com.mlm.mlmapp.utils.CommonUtils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.ColorRes;
import org.androidannotations.annotations.res.DimensionRes;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

@EActivity(R.layout.coupon_list)
public class CouponListActivity extends BaseActivity implements HTTPCons {

	@Pref
	Prefs_ pref;
	@DimensionRes(R.dimen.recyclerview_mycomments_space)
	float commentImagesMargin;
	@DimensionRes
	float comments_images_edge;
	@ColorRes
	int red;
	@ColorRes
	int blacktext;
	public String currentJson = "";
	@ViewById
	TextView tv_title;
	@ViewById
	TextView tv_no_title;
	private AQuery aq;
	private CouponListAdapter adapter;
	List<JSONObject> couponListDataset;
	@ViewById
	RecyclerView rv_coupons;
	@Extra
	String couponid;
	@Extra
	String id;

	@AfterViews
	public void init() {
		String title=getTitle().toString();
		if(!"".equals(title)){
			((TextView) this.findViewById(R.id.tv_title)).setText(title);
		}
		
		aq = new AQuery(this);
		aq.hardwareAccelerated11();
		couponListDataset = new ArrayList<>();
		final GridLayoutManager layoutManager = new SyGridLayoutManager(this, 1);
		rv_coupons.setLayoutManager(layoutManager);
		rv_coupons
				.addItemDecoration(new GridMarginDecoration(this, 0, 0, 0, 0));
		rv_coupons.setItemAnimator(new RecyclAnimation());
		adapter = new CouponListAdapter(CouponListActivity.this,
				couponListDataset);

		rv_coupons.setAdapter(adapter);
		getCouponAction();
		
	}

	void getCouponAction() {
		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
		pairs.add(new BasicNameValuePair("mobile", pref.loginID().get()));
		pairs.add(new BasicNameValuePair("orderId", id));
		CommonUtils.postRequest4Array(get_user_coupon, aq,
				new AjaxCallback<JSONArray>() {

					@Override
					public void callback(String url, JSONArray array,
							AjaxStatus status) {
						if (status.getCode() == -101) {
							Toast.makeText(aq.getContext(),
									"网络异常,请检查网络", Toast.LENGTH_SHORT);
							return;
						}
						// TODO Auto-generated method stub
						if (status.getCode() == 200) {

							for (int i = 0; i < array.length(); i++) {
								JSONObject obj;
								try {
									obj = array.getJSONObject(i);
									couponListDataset.add(obj);
								} catch (JSONException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}

							}
							adapter.notifyDataSetChanged();
							
						} else if(status.getCode() == 500){
							Toast.makeText(aq.getContext(),
									"服务器异常:" + status.getCode(),
									Toast.LENGTH_SHORT).show();
						}else{
							Toast.makeText(aq.getContext(),
									"网络异常:" + status.getCode(),
									Toast.LENGTH_SHORT).show();
						}
					}

				}, pairs);
	}

	class CouponListAdapter extends
			RecyclerView.Adapter<RecyclerView.ViewHolder> {
		private Context mContext;
		private List<JSONObject> array;
		private List<RadioButton> rlist;

		public CouponListAdapter(Context con, List<JSONObject> array) {
			this.mContext = con;
			this.array = array;
			rlist = new ArrayList<RadioButton>();
			
		}

		@Override
		public int getItemCount() {
			return array.size();
		}

		public boolean isPositionHeader(int position) {
			return position == 0;
		}

		@Override
		public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
				int viewType) {
			// TODO Auto-generated method stub
			final CouponViewHolder holder = new CouponViewHolder(LayoutInflater
					.from(CouponListActivity.this).inflate(
							R.layout.coupon_mine_list_item, parent, false));
			return holder;

		}

		private void radioGroupChecked(CompoundButton buttonView) {
			for (RadioButton rb : rlist) {
				if ((int) rb.getTag() != (int) buttonView.getTag()) {
					rb.setChecked(false);
				}

			}
		}

		@Override
		public void onBindViewHolder(RecyclerView.ViewHolder holder,
				int position) {
			// TODO Auto-generated method stub
			final CouponViewHolder vholder = (CouponViewHolder) holder;
			final JSONObject obj =array.get(position);
			final Coupon c =new Coupon();
			c.setId(getJsonString(obj, "id"));
			c.setDate(getJsonString(obj, "endDate"));
			c.setName(getJsonString(obj, "name"));
			c.setStylistId(getJsonString(obj, "stylistId"));
			c.setCouponValues(getJsonString(obj, "couponValues"));
			c.setDescription(getJsonString(obj, "description"));
			c.setType(getJsonString(obj, "type"));
			c.setStatus(getJsonString(obj,"status"));
				vholder.tv_date.setText(c.getDate());
			vholder.tv_intro.setText(c.getDescription());
			vholder.tv_name.setText(c.getName());
			vholder.tv_price.setText(c.getCouponValues());
			vholder.btn_appointment.setVisibility(View.INVISIBLE);
			vholder.middle.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View view) {
					itemClicked(obj);

				}
			});
		}

		public class CouponViewHolder extends RecyclerView.ViewHolder {
			TextView tv_name;
			TextView tv_price;
			TextView tv_date;
			TextView tv_intro;
			Button btn_appointment;
			ImageView left;
			ImageView right;
			RelativeLayout middle;
			public CouponViewHolder(View itemView) {
				super(itemView);
				tv_name = (TextView) itemView.findViewById(R.id.tv_name);
				tv_price = (TextView) itemView.findViewById(R.id.tv_price);
				tv_date = (TextView) itemView.findViewById(R.id.tv_date);
				tv_intro = (TextView) itemView.findViewById(R.id.tv_intro);
				btn_appointment = (Button) itemView.findViewById(R.id.btn_appointment);
				left = (ImageView) itemView.findViewById(R.id.left);
				right = (ImageView) itemView.findViewById(R.id.right);
				middle = (RelativeLayout) itemView.findViewById(R.id.middle);

			}
		}
	}
	void itemClicked(JSONObject temp){
		Intent data = new Intent();
		data.putExtra("jsonString", temp.toString());
		CouponListActivity.this.setResult(
				RESULT_OK, data);
		CouponListActivity.this.finish();
	}
	@Click
	void btn_back() {
		onBackPressed();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		Intent data = new Intent();
		setResult(RESULT_CANCELED, data);
		this.finish();
	}
	public class Coupon {
		private String id;

		public String getCouponValues() {
			return couponValues;
		}

		public void setCouponValues(String couponValues) {
			this.couponValues = couponValues;
		}

		private String stylistId;
		private String name;
		private String date;
		private String couponValues;
		private String type;

		public String getStatus() {
			return status;
		}

		public void setStatus(String status) {
			this.status = status;
		}

		private String status;
		public String getStylistId() {
			return stylistId;
		}

		public void setStylistId(String stylistId) {
			this.stylistId = stylistId;
		}

		public String getType() {
			return type;
		}

		public void setType(String type) {
			this.type = type;
		}

		public String getDescription() {
			return description;
		}

		public void setDescription(String description) {
			this.description = description;
		}

		private String description;
		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getDate() {
			return date;
		}

		public void setDate(String date) {
			this.date = date;
		}

	}
}
